<?php

namespace app\api\controller;

use app\common\model\Game as GameModel;
use app\common\model\User as UserModel;
use app\common\model\Fight as FightModel;
use app\common\model\GameJoin as GameJoinModel;
use app\common\model\GameJoin;
use app\service\MsgService;
use think\Exception;

class Game extends Base
{

    //信息
    public function game()
    {
        if (request()->isPost()) {
            $data = input('');
            $page = input('page', 1);
            $size = input('size', 20);
            $where = [
                'end_time' => ['>', date('Y-m-d H:i:s')],
                'status' => 1,
            ];
            $list = GameModel::getPage($where, $page, $size, 'start_time asc');
            if ($list) {
                foreach ($list as $key => $value) {
                    $fightjoin = GameJoinModel::getColumn(['gid' => $value['id']], 'fid', 'id asc');
                    $team = [];
                    if ($fightjoin) {
                        $team = FightModel::getColumn(['id' => ['in', $fightjoin]], 'name');
                    }
                    switch (sizeof($team)) {
                        case 1:
                            $team[] = '？';
                            break;
                        case 0:
                            $team[] = '？';
                            $team[] = '？';
                            break;
                    }
                    $list[$key]['team'] = $team;
                }
            }
            return returnJson(200, 'success', $list);
        }
    }

    public function game_edit()
    {
        if (request()->isPost()) {
            $data = input('');
            if (!$data['name']) {
                return returnJson(202, '请输入赛事名称');
            }
            if (!$data['start_time'] || !$data['end_time']) {
                return returnJson(202, '请选择赛事进行的时间');
            }
            if (!$data['address']) {
                return returnJson(202, '请输入赛事进行的地址');
            }
//            if(!$data['contact']){
//                return returnJson(202,'请输入赛事负责人');
//            }
//            if(!$data['phone']){
//                return returnJson(202,'请输入赛事负责人联系方式');
//            }
            $fight = FightModel::getOne(['id' => $data['fid']]);
            $save_data = [
                'name' => $data['name'],
                'uid' => $this->uid,
                'start_time' => $data['start_time'],
                'end_time' => $data['end_time'],
                'contact' => $fight['contact'],
                'phone' => $fight['phone'],
                'address' => $data['address'],
                'update_time' => time(),
                'create_time' => time(),
            ];
            $res = GameModel::create($save_data);
            if ($res) {
                //新赛事通知管理员
                $game_manager = explode(',', config('game_manager'));
                if ($game_manager && is_array($game_manager)) {
                    foreach ($game_manager as $value) {
                        $save_data['id'] = $res;
                        $save_data['first'] = '新赛事提交审核通知';
                        $save_data['remark'] = "有新的赛事提交审核。\n赛事负责人：".$save_data['contact']."\n联系方式：".$save_data['phone'];
                        MsgService::S()->sendTemp(MsgService::$tempType_NEW_GAME, $this->mall_id, $value, $save_data);
                    }
                }

                $fight_data = [
                    'gid' => $res,
                    'fid' => $data['fid'],
                    'update_time' => time(),
                    'create_time' => time(),
                    'uid' => $this->uid
                ];
                GameJoin::create($fight_data);
                return returnJson(200, '赛事创建成功，审核中...');
            } else {
                return returnJson(202, '赛事创建失败');
            }
        }
    }

    //信息
    public function game_detail()
    {
        if (request()->isPost()) {
            $data = input('');
            $where = [
                'id' => $data['id']
            ];
            $list = GameModel::getOne($where);
            $fids = GameJoinModel::getColumn(['gid' => $data['id']], 'fid', 'id asc');
            $team = FightModel::getAll(['id' => ['in', $fids]], 'id asc');
            $list['team'] = $team;
            return returnJson(200, 'success', $list);
        }
    }

}
